package com.lovo.ssc.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.ssc.bean.AddressBean;
import com.lovo.ssc.service.IAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户的收货地址 前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-06
 */
@RestController
@RequestMapping("/address")
@Api(tags = "收货地址接口(刘洪刚)")
public class AddressController {
    @Autowired
    private IAddressService addressService;

    @GetMapping("exportExcel/{userId}")
    @ApiOperation(value = "导出收货地址")
    public void exportExcel(@PathVariable("userId") Integer userId, HttpServletResponse response){
        try {

            addressService.exportExcel(userId, response);
        } catch (IOException e) {
            e.printStackTrace();

        }
    }


    @PostMapping("/importExcel/{userId}")
    @ApiOperation(value = "导入收货地址，Excel表头（序号、收货人、地区、详细地址、收货人电话）")
    public String importExcel(@PathVariable("userId") Integer userId,@RequestParam("file") MultipartFile file){
        ModelAndView modelAndView = new ModelAndView();
        try {
            addressService.importExcel(userId,file);
        } catch (Exception e) {
            modelAndView.addObject("msg", e.getMessage());
            e.printStackTrace();
            return e.getMessage();
        }
        return "导入成功";
    }

    @PostMapping("updateAddress")
    @ApiOperation(value = "修改收货地址")
    public boolean updateAddress(AddressBean addressBean){
        if (addressService.updateById(addressBean)){
            return true;
        }
        return false;
    }

    @GetMapping("delAddress/{addressId}")
    @ApiOperation(value = "按收货地址编号删除收货地址")
    public boolean delAddress(@PathVariable("addressId") Integer addressId){

        if (addressService.removeById(addressId)){
            return true;
        }
        return false;
    }


    @ApiOperation(value = "按用户编号添加收货地址")
    @PostMapping("addAddress/{userId}")
    public boolean addAddress(@PathVariable("userId") Integer userId, AddressBean addressBean){
        addressBean.setUserId(userId);
        if (addressService.save(addressBean)){
            return true;
        }
        return false;
    }

    @GetMapping("findAllAddress/{userId}")
    @ApiOperation(value = "按用户编号查询所有收货地址")
    public List<AddressBean> findAllAddress(@PathVariable("userId") Integer userId){


        return addressService.findAllAddress(userId);
    }

    @GetMapping("findDefaultAddressByUserId/{userId}")
    @ApiOperation(value = "按用户编号查询默认收货地址")
    public AddressBean findDefaultAddressByUserId(@PathVariable("userId")Integer userId){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("userId", userId);
        qw.eq("defaultAddress", "1");

        return addressService.getOne(qw);
    }
    @GetMapping("updateDefaultAddress/{userId}/{addressId}")
    @ApiOperation(value = "设置默认收货地址")
    public boolean updateDefaultAddress(@PathVariable("userId")Integer userId,@PathVariable("addressId")Integer addressId){
        //将旧的默认地址取消
        QueryWrapper qw = new QueryWrapper();
        qw.eq("userId", userId);
        qw.eq("defaultAddress", "1");
        AddressBean oldAddressBean = addressService.getOne(qw);

        AddressBean newAddressBean = addressService.getById(addressId);
        if (oldAddressBean==null){
            //设置新的默认地址
            newAddressBean.setDefaultAddress("1");

            return addressService.updateById(newAddressBean);
        }else {
            oldAddressBean.setDefaultAddress("0");
            addressService.updateById(oldAddressBean);
            //设置新的默认地址
            newAddressBean.setDefaultAddress("1");

            return addressService.updateById(newAddressBean);
        }
    }

    /**
     * 查询除了默认收货地址的其他所有地址
     * @return
     */
    @GetMapping("findAddressExceptDefault/{userId}")
    public List<AddressBean> findAddressExceptDefault(@PathVariable("userId") Integer userId){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("defaultAddress", "0");
        qw.eq("userId", userId);
        return addressService.list(qw);
    }
}
